package com.wasion.gydpe.ice;

import com.wasion.gydpe.ice.proxy.publishsubscriber.MessagePublishSubscriber;

public class Ice_Push_Thread implements Runnable {

	private static InitServer instance = null;

	public synchronized static InitServer getInstance() {
		if (instance == null)
			instance = new InitServer();
		return instance;

	}

	public void run() {

		int status = 0;
		Ice.Communicator ic = null;
		try {
			ic = Ice.Util.initialize();
			String ServerTopic = "";
			String IceStorm_adapter = "";
			String IceStorm_port = "";
			String IceStrom_manager = "";
			TestProperties ec = TestProperties.getInstance();
			if (ec != null) {
				ServerTopic = ec.getPropertyValue("/IceComm.properties",
						"IceServer.Storm");
				IceStorm_adapter = ec.getPropertyValue("/IceComm.properties",
						"IceServer.MessagePublishSubscriberAdapter");
				IceStorm_port = ec.getPropertyValue("/IceComm.properties",
						"IceServer.StormPort");
				IceStrom_manager = ec.getPropertyValue("/IceComm.properties",
						"IceServer.StormManager");
			}

			Ice.ObjectPrx base = ic.stringToProxy(ServerTopic);
			if (base == null) {
				throw new RuntimeException("Cannot create proxy");
			}

			IceStorm.TopicManagerPrx topicManager = IceStorm.TopicManagerPrxHelper
					.checkedCast(base);
			if (topicManager == null) {
				throw new RuntimeException("Invalid proxy");
			}

			Ice.ObjectAdapter adapter = ic.createObjectAdapterWithEndpoints(
					IceStorm_adapter, IceStorm_port);
			MessagePublishSubscriber monitor = new publishI();
			Ice.ObjectPrx proxy = adapter.addWithUUID(monitor);
			IceStorm.TopicPrx topic = null;
			try {
				topic = topicManager.retrieve(IceStrom_manager);
				java.util.Map qos = null;
				topic.subscribe(qos, proxy);
			} catch (IceStorm.NoSuchTopic ex) {
				ex.printStackTrace();
			}
			adapter.activate();
			ic.waitForShutdown();
			topic.unsubscribe(proxy);
		} catch (Ice.LocalException e) {
			e.printStackTrace();
			status = 1;
		} catch (Exception e) {
			status = 1;
		} finally {
			if (ic != null)
				ic.destroy();
		}
		System.exit(status);
	}
}
